# Copyright (c) 2008-2009 Mikhail Chernov 
#
# This file is part of dcExplorer.
#
# dcExplorer is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# dcExplorer is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with dcExplorer.  If not, see <http://www.gnu.org/licenses/>.

package dcExplorer::canvas::Generic;
use strict;

sub new
{
	my $proto = shift;
	my $class = ref($proto) || $proto;
	my $self  = {};
	$self->{Image} = shift;
	$self->{min_x} = shift;
	$self->{min_y} = shift;
	$self->{max_x} = shift;
	$self->{max_y} = shift;
	$self->{bsize} = shift;
	$self->{bord_x} = shift;
	$self->{bord_y} = shift;
	$self->{size_x} = ($self->{max_x} - $self->{min_x} + 1) * $self->{bsize} + 2 * $self->{bord_x};
	$self->{size_y} = ($self->{max_y} - $self->{min_y} + 1) * $self->{bsize} + 2 * $self->{bord_y};
	bless ($self, $class);
	return $self;
}

sub shiftX
{
	my $self = shift;
	my $x = shift;
	return $x - $self->{min_x};
}

sub shiftY
{
	my $self = shift;
	my $y = shift;
	return $y - $self->{min_y};
}

sub centerX
{
	my $self = shift;
	my $x = shift;
	my $dx = $self->shiftX($x);
	$dx -= 2000 if $dx > 1000;
	$dx += 2000 if $dx < -1000;
	return ($dx + 0.5) * $self->{bsize} + $self->{bord_x};
}

sub centerY 
{
	my $self = shift;
	my $y = shift;
	my $dy = $self->shiftY($y);
	$dy -= 2000 if $dy > 1000;
	$dy += 2000 if $dy < -1000;
	return ($dy + 0.5) * $self->{bsize} + $self->{bord_y};
}

sub center
{
	my $self = shift;
	my ($x, $y) = @_;
	return ($self->centerX($x), $self->centerY($y));
}

sub rect
{
	my $self = shift;
	my ($x, $y) = @_;
	my $dx = $self->shiftX($x);
	my $dy = $self->shiftY($y);
	return ($dx * $self->{bsize} + $self->{bord_x},
		$dy * $self->{bsize} + $self->{bord_y},
		($dx + 1) * $self->{bsize} + $self->{bord_x} - 1,
		($dy + 1) * $self->{bsize} + $self->{bord_y} - 1)
}

1;
